﻿using CgdataBase;
using Prism.Commands;
using System.ComponentModel;
using System.Windows;
using System.Windows.Media;

namespace FilterCopyer.Views
{
    public partial class InputBox : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler? PropertyChanged;

        private AppSettings Settings => AppSettings.Instance;

        public string Message { get; set; }

        public string Value { get; set; }
        public int ValueFocused { get; set; }

        public DelegateCommand SubmitCommand { get; }

        public InputBox()
        {
            InitializeComponent();
            this.FontSize = Settings.FontSize;
            this.FontFamily = new FontFamily(Settings.FontName);
            DataContext = this;

            SubmitCommand = new DelegateCommand(OnSubmit);

            this.Loaded += (s, e) => ValueFocused++;
        }

        private void OnSubmit()
        {
            if (Value.IsNullOrEmpty())
            {
                MessageBox.Show("请先输入内容");
                ValueFocused++;
                return;
            }

            this.DialogResult = true;
        }

        public static string Show(string message, string title = "输入框", string value = "", Window owner = null)
        {
            return App.Current.Dispatcher.Invoke<string>(() =>
            {
                var win = new InputBox() { Message = message, Title = title, Value = value };
                if (owner != null)
                {
                    win.Owner = owner;
                    win.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                }
                else
                {
                    win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                }
                if (win.ShowDialog() == true)
                {
                    return win.Value;
                }

                return string.Empty;
            });
        }

        public static string Show(string message, Window owner = null)
        {
            return Show(message, "输入框", "", owner);
        }
    }
}